package com.ai.smlcore.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 时间工具类
 */
public final class DateUtil {
	/** Private Constructor **/
	private DateUtil() {
	}

	/** 日期格式 **/
	public interface DATE_PATTERN {
		String HHMMSS = "HHmmss";
		String HH_MM_SS = "HH:mm:ss";
		String HH_MM = "HH:mm";
		String YYYY = "yyyy";
		String YYYYMMDD = "yyyyMMdd";
		String YYYY_MM_DD = "yyyy-MM-dd";
		String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
		String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
		String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	}

	/** 获取当前日期 */
	public static int getCurrentDay() {
		String day = date2String(new Date(), DATE_PATTERN.YYYYMMDD);
		return Integer.valueOf(day);
	}
	
	/** 获取当前日期 */
	public static int dateAdd(long time, long add) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis() + add);
		String day = date2String(calendar.getTime(), DATE_PATTERN.YYYYMMDD);
		return Integer.valueOf(day);
	}
	
	/**
	 * 将Date类型转换成String类型
	 * 
	 * @param date
	 *            Date对象
	 * @return 形如:"yyyy-MM-dd HH:mm:ss"
	 */
	public static String date2String(Date date) {
		return date2String(date, DATE_PATTERN.YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 将Date按格式转化成String
	 * 
	 * @param date
	 *            Date对象
	 * @param pattern
	 *            日期类型
	 * @return String
	 */
	public static String date2String(Date date, String pattern) {
		if (date == null || pattern == null){
			return null;
		}
		return new SimpleDateFormat(pattern).format(date);
	}
	
	/**
	 * 将String类型转换成Date类型
	 * 
	 * @param date
	 *            Date对象
	 * @return
	 */
	public static Date string2Date(String date) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN.YYYY_MM_DD_HH_MM_SS);
		try {
			return format.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}
	
	/**
	 * 获取某日期N天后的日期
	 * @param datestr
	 * @param day
	 * @return
	 */
	public static Date getBeforeAfterDate(String datestr, int day) {  
        SimpleDateFormat df = new SimpleDateFormat(DATE_PATTERN.YYYY_MM_DD_HH_MM_SS);  
        java.sql.Date olddate = null;  
        try {  
            df.setLenient(false);  
            olddate = new java.sql.Date(df.parse(datestr).getTime());  
        } catch (ParseException e) {  
            throw new RuntimeException("日期转换错误");  
        }  
        Calendar cal = new GregorianCalendar();  
        cal.setTime(olddate);  
  
        int Year = cal.get(Calendar.YEAR);  
        int Month = cal.get(Calendar.MONTH);  
        int Day = cal.get(Calendar.DAY_OF_MONTH);  
  
        int NewDay = Day + day;  
  
        cal.set(Calendar.YEAR, Year);  
        cal.set(Calendar.MONTH, Month);  
        cal.set(Calendar.DAY_OF_MONTH, NewDay);  
  
        return new Date(cal.getTimeInMillis());  
    }  

    /**
     * 计算两个日期差的天数
     * 
     * @param fDate
     * @param oDate
     * @return
     */
    public static int daysBetween(Date fDate, Date oDate) {
        Calendar cNow = Calendar.getInstance();
        Calendar cReturnDate = Calendar.getInstance();
        cNow.setTime(oDate);
        cReturnDate.setTime(fDate);
        cNow.set(Calendar.HOUR_OF_DAY, 0);
        cNow.set(Calendar.MINUTE, 0);
        cNow.set(Calendar.SECOND, 0);
        cReturnDate.set(Calendar.HOUR_OF_DAY, 0);
        cReturnDate.set(Calendar.MINUTE, 0);
        cReturnDate.set(Calendar.SECOND, 0);
        long todayMs = cNow.getTimeInMillis();
        long returnMs = cReturnDate.getTimeInMillis();
        long intervalMs = todayMs - returnMs;
        return (int)(intervalMs / (1000 * 86400));
    }
    
    /**
     * @Description: 获取当前日期的前一天
     * @return
     * @ReturnType String
     * @author: liyl
     * @Created 2015年11月13日 下午5:11:14
     */
    public static Date currentBeforeDay(){
    	Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, -1);
    	return calendar.getTime();
    }
    
    /**
     * @Description: 获取当前日期的后一天
     * @return
     * @ReturnType Date
     * @author: liyl
     * @Created 2015年11月13日 下午5:14:54
     */
    public static Date currentNextDay(){
    	Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, 1);
    	return calendar.getTime();
    }
    
    /**
     * 获取时间的星期
     * @param dt
     * @return
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"SUND_FLAG", "MOND_FLAG", "TUES_FLAG", "WEDN_FLAG", "THUR_FLAG", "FRID_FLAG", "SATU_FLAG"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
    
    /**
     * 时间比大小
     * @param DATE1
     * @param DATE2
     * @param pattern
     * @return
     */
    public static int compareDate(String DATE1, String DATE2,String pattern) {
        
        DateFormat df = new SimpleDateFormat(pattern);
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
    
    /**
	 * 在一个时间上加上或减去分钟
	 * 
	 * @param ti
	 *            long
	 * @param i
	 *            int
	 * @return Date
	 */
	public static Date addOrMinusMinutes(Date date, int i) {
		Date rtn = null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(GregorianCalendar.MINUTE, i);
		rtn = cal.getTime();
		return rtn;
	}
	/**
	 * 
	 * 日期格式转化
	 * 
	 * @author 
	 * @date 2009-3-26 下午01:36:29
	 * @version: V1.0
	 * 
	 * @param srcDate
	 *            源日期字符串
	 * @param srcFormat
	 *            源日期格式，默认为yyyy-MM-dd
	 * @param destFormat
	 *            目的日期格式，默认为yyyy-MM-dd
	 * @return 转换格式后的日期字符串。当srcDate为null或 空字符串时，则返回空字符串；当srcDate解析错误时，则返回当前日期字符串
	 */
	public static String dateFormat(String srcDate, String srcFormat, String destFormat) {
		if (null == srcFormat || "".equals(srcFormat)) {
			srcFormat = "yyyy-MM-dd";
		}

		if (null == destFormat || "".equals(destFormat)) {
			destFormat = "yyyy-MM-dd";
		}

		if (null == srcDate || "".equals(srcDate)) {
			return "";
		}

		Calendar cal = Calendar.getInstance();

		Date srcFormatDate = null;

		try {
			srcFormatDate = (Date) new SimpleDateFormat(srcFormat).parse(srcDate);
		} catch (ParseException e) {
		//	logger.error("", e);
			srcFormatDate = new Date();
		}

		cal.setTime(srcFormatDate);

		return new SimpleDateFormat(destFormat).format(cal.getTime());

	}
	public static void main(String[] args) throws Exception {
		 System.out.println(DateUtil.date2String(getBeforeAfterDate(DateUtil.date2String(new Date()),90)));
	}
	   
}
